package com.itcat.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itcat.mysql.pojo.UmsAdmin;
import com.itcat.mysql.pojo.UmsRole;
import com.itcat.mysql.vo.RoleCount;
import org.apache.ibatis.annotations.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Cr Mr]
 * @version : [v1.0]
 * @date : [2022/3/31 19:34]
 */
@Mapper
public interface UmsAdminRoleMapper extends BaseMapper<UmsRole> {

    @Select("SELECT * FROM ums_role WHERE id IN (SELECT role_id from ums_admin_role_relation where admin_id=#{adminId})")
    List<UmsRole> selectRole(Long adminId);

    /***
     * 给角色分配菜单
     * @author Cr Mr
     * @date 2022/4/3 13:01
     */
    @Insert("<script>" +
            "INSERT INTO ums_role_menu_relation (role_id,menu_id) values" +
            "<foreach collection=\"items\" index=\"index\" item=\"item\" separator=\",\"> " +
            "(#{roleId},#{item})" +
            "</foreach> " +
            "</script>")
    void allocMenu(@Param("roleId") Long roleId, @Param("items") List<Long> items);

    /***
     * 批量删除角色关联的菜单
     * @author Cr Mr
     * @date 2022/4/3 12:59
     */
    @Delete("<script>" +
            "DELETE FROM ums_role_menu_relation WHERE role_id in" +
            "<foreach item = 'item' index = 'index' collection = 'items' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    void deleteMenu(@Param("items") List<Long> items);

    /***
     * 批量删除角色关联的资源
     * @author Cr Mr
     * @date 2022/4/3 12:59
     */
    @Delete("<script>" +
            "DELETE FROM ums_role_resource_relation WHERE role_id in" +
            "<foreach item = 'item' index = 'index' collection = 'items' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    void deleteResource(@Param("items") List<Long> items);


    @Select("SELECT role_id,count(*) as count from ums_admin_role_relation group by role_id")
    List<RoleCount> selectRoleAdminCount();

    @Update("<script>" +
            "<foreach item = 'item' index = 'index' collection = 'items' open='' separator=';' close=''>" +
            "UPDATE ums_role" +
            "<set>" +
            "admin_count = #{item.count}," +
            "</set>" +
            "where id = #{item.roleId,jdbcType=BIGINT}" +
            "</foreach>" +
            "</script>")
    void setRoleAdminCount(@Param("items") List<RoleCount> items);

    @Select("SELECT * FROM ums_role WHERE status=1 AND id IN (SELECT role_id from ums_admin_role_relation where admin_id=#{adminId})")
    List<UmsRole> getUmsRole(Long umsAdmin);
}
